package tdd.kata.day1;

/** @author robin */
public class Day2 {
  String[] commands;
  int horizontal = 0;
  int depth = 0;
  int aim = 0;
  public Day2(String[] commands) {
    this.commands = commands;
  }

  public void pilot() {
    for (String command : commands) {
      CommandPlanned commandPlanned = commandToPlanned(command);
      if (commandPlanned.isHorizontal()) {
        horizontal += commandPlanned.horizontal();
      } else {
        depth += commandPlanned.depth();
      }
    }
  }

  public void pilotTwo() {
    for (String command : commands) {
      CommandPlanned commandPlanned = commandToPlanned(command);
      if (commandPlanned.isHorizontal()) {
        horizontal += commandPlanned.horizontal();
        depth += (commandPlanned.horizontal() * aim);
      } else {
        aim += commandPlanned.depth();
      }
    }
  }

  private CommandPlanned commandToPlanned(String command) {
    String[] commands = command.split(" ");
    return new CommandPlanned(commands[0], Integer.parseInt(commands[1]));
  }

  public int getHorizontal() {
    return horizontal;
  }

  public int getDepth() {
    return depth;
  }

  public int getAim() {
    return aim;
  }

  public int getFinalPosition() {
    return horizontal * depth;
  }

  class CommandPlanned {
    String command;
    int value;

    public CommandPlanned(String command, int value) {
      this.command = command;
      this.value = value;
    }

    public int horizontal() {
      return value;
    }

    public int depth() {
      if ("down".equals(command)) {
        return value;
      } else {
        return -value;
      }
    }

    public boolean isHorizontal() {
      return ("forward".equals(command));
    }
  }
}
